package com.gugee.tiktok.web.repository.impl;

import com.gugee.tiktok.web.dto.BloggerAwemeResponse;
import com.gugee.tiktok.web.repository.AwemeExtRepository;
import com.xiguaji.boot.toolkit.jdbc.JdbcTemplateHelper;
import com.xiguaji.boot.toolkit.jdbc.Page;

import java.util.ArrayList;
import java.util.List;

public class AwemeExtRepositoryImpl implements AwemeExtRepository {

    @Override
    public Page<BloggerAwemeResponse> getPage(long id, SearchType searchType, int page, int pageSize, long startTime, long endTime, int sort) {
        List<Object> params = new ArrayList<>();
        String selectSql = "SELECT id,o_cover cover,o_aweme_id aweme_id,o_dynamic_cover dynamic_cover," +
                "o_share_url share_url,o_desc title,o_create_time create_time," +
                "score,o_play_count views,o_digg_count likes," +
                "o_comment_count reviews,o_share_count shares,o_duration as duration," +
                "cast((( o_digg_count + o_share_count + o_comment_count )/  o_play_count * 100) AS DECIMAL (3,1)) engagement_rate ";
        StringBuffer sql = new StringBuffer("FROM aweme ");

        if(searchType.getValue() == SearchType.BLOGGER.getValue()){
            sql.append("WHERE o_uid = ? ");
        }else if(searchType.getValue() == SearchType.MUSIC.getValue()){
            sql.append("WHERE o_music_id = ? ");
        }
        params.add(id);
        sql.append("AND o_create_time BETWEEN ? AND ? ");
        params.add(startTime);
        params.add(endTime);
        String orderBy;
        switch (sort){
            case 2:
                orderBy = "ORDER BY score desc ";
                break;
            case 3:
                orderBy = "ORDER BY o_play_count desc ";
                break;
            case 4:
                orderBy = "ORDER BY o_digg_count desc ";
                break;
            case 5:
                orderBy = "ORDER BY o_comment_count desc ";
                break;
            case 6:
                orderBy = "ORDER BY o_share_count desc ";
                break;
            default:
                orderBy = "ORDER BY o_create_time desc ";
                break;
        }
        sql.append(orderBy);
        return JdbcTemplateHelper.paginate(BloggerAwemeResponse.class,page,pageSize,selectSql,sql.toString(),params.toArray());
    }
}
